package com.service;

import com.pojo.Permission;
import com.pojo.Role;
import com.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.parameters.P;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class UserService implements UserDetailsService {

    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;


    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        User user = findByUserName(s);
        //1.根据用户名查询数据库获得用户信息（包含数据库中存储的密码信息）
        if(user!=null){
            //2.将用户信息返回给框架,框架会进行密码比对（比对页面传递过来的）
            String userName = user.getUserName();
            String userPassword =user.getUserPassword();
            List<GrantedAuthority> grantedAuthorities=new ArrayList<>();
            List<Role> roles = user.getRoles();
            roles.forEach(r->{
                grantedAuthorities.add(new SimpleGrantedAuthority(r.getName()));

                List<Permission> permission = r.getPermissionList();
                permission.forEach(p->{
                    grantedAuthorities.add(new SimpleGrantedAuthority(p.getName()));
                });
            });
            org.springframework.security.core.userdetails.User securityUser=new org.springframework.security.core.userdetails.User(userName,userPassword,grantedAuthorities);
            return securityUser;
        }
        return null;
    }

    public User findByUserName(String name){
        if("admin".equals(name)){
            User user=new User();
            user.setUserName("admin");
            user.setUserPassword(bCryptPasswordEncoder.encode("admin"));

            Role role=new Role();
            role.setName("ROLE_ADMIN");
            Permission permission=new Permission();
            permission.setName("ADD_CHECKITEM");
            //把权限加进角色里
            role.getPermissionList().add(permission);

            List<Role> roleList=new ArrayList<>();
            roleList.add(role);
            //把角色加进用户里
            user.setRoles(roleList);
            return user;
        }else if("xiaoming".equals(name)){
            User user=new User();
            user.setUserName("xiaoming");
            user.setUserPassword(bCryptPasswordEncoder.encode("xm"));

       /*     Role role=new Role();
            role.setName("ROLE_ADMIN");
            List<Role> roleList=new ArrayList<>();
            roleList.add(role);
            //把角色加进用户里
            user.setRoles(roleList);*/

            return user;
        }
        return null;
    }


}
